<template>
	<div class="">
		<jk-card>
			<jk-table ref="jkTable" :loading="tableLoading" :data="tableData" :height="height">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<jk-state-bar v-if="item.keyName === 'upkeepState'" :state="scope.row.upkeepState" :state-list="upkeepStateList"></jk-state-bar>
						<span v-else-if="item.keyName === 'cycleUnit'">{{ transferCircleUnit(scope.row) }}</span>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="120">
					<template slot-scope="scope">
						<div class="flex-center">
							<!--<div class="item-gap"><el-button size="mini" type="success" :disabled="scope.row.upkeepState > 1" @click="onTakeBtn(scope.row)">保养签到</el-button></div>-->
							<!--<el-button size="mini" class="item-gap" :disabled="scope.row.upkeepState > 2" type="danger" @click="onEditBtn(scope.row)">完成保养</el-button>-->
							<el-dropdown trigger="click" @command="onDropdownChange($event, scope.row)">
								<el-button type="primary" size="mini" class="el-dropdown-link">
									更多<i class="el-icon-arrow-down el-icon--right"></i>
								</el-button>
								<el-dropdown-menu slot="dropdown">
									<el-dropdown-item
										v-for="(item) in dropdownOption"
										:key="item.code"
										:command="item.code"
										:divided="item.divided"
										:disabled="item.allowState || item.allowState === 0 ? scope.row.upkeepState !== item.allowState : false"
										:style="{
											'display': !item.isShow ? 'none' : 'block',
											'text-decoration': getPermission([`${prefix}:${item.permissionName}`]) ? '' : 'line-through'
										}"
									>
										<span :class="`${item.icon} item-gap`"></span>
										<span>{{ item.name }}</span>
									</el-dropdown-item>
								</el-dropdown-menu>
							</el-dropdown>
						</div>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box3" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<detail-dialog
			:data-id="dataId"
			:dialog-state="detailDialogState"
			@on-visible-change="onVisibleChangeOfDetailDialog"
		></detail-dialog>
		<save-dialog
			:data-id="dataId"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></save-dialog>
		<jump-dialog
			:data-id="dataId"
			:dialog-state="jumpDialogState"
			@on-visible-change="onVisibleChangeOfJumpDialog"
			@on-finish="onFinishOfJumpDialog"
		></jump-dialog>
	</div>
</template>

<script>
    import saveDialog from '../../save-dialog';
    import jumpDialog from '../../jump-dialog';
    import detailDialog from '../../detail-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { firstCharacterToLowerCase, pageConfig, tipsBar } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../../../../mixins/getMenuColumn';
    import { upkeepStateList } from '../../../../../../utils/jk/status-option-list';
    import {
        machineManagerUpkeepMachineJobClearComplete,
        machineManagerUpkeepMachineJobClearTaking,
        machineManagerUpkeepMachineJobDelete,
        machineManagerUpkeepMachineJobPage,
        machineManagerUpkeepMachineJobTaking
    } from '../../../../../../api/machine/upkeepTask';
    import permissionMixin from '../../../../../../mixins/permission-mixin';
    import upkeepMixin from '../../upkeep';
    import { systemUserColumnConfigGet } from '../../../../../../api/system/column-config';
    export default {
        name: 'UpkeepTask',
        props: {
            queryParams: {
                type: Object,
                default() {
                    return {};
                }
            },
            height: {
                type: Number,
                default: 0
            }
        },
        components: {
            jumpDialog,
            saveDialog,
            detailDialog
        },
        mixins: [calHeight, getMenuColumn, permissionMixin, upkeepMixin],
        data() {
            return {
                prefix: 'machinemanager:upkeepMachineJob',
                apiItem: {
                    'page': machineManagerUpkeepMachineJobPage,
                    'delete': machineManagerUpkeepMachineJobDelete
                },
                dropdownOption: [
                    { code: 'DETAIL', name: '详情', isShow: true, permissionName: 'query', icon: 'el-icon-view', allowState: null, api: null }
                    // { code: 'JUMP', name: '跳过', permissionName: 'update', isShow: true, icon: 'el-icon-top-right', allowState: 0, api: null },
                    // { code: 'CANCEL_CLOSE', name: '取消签到', permissionName: 'update', isShow: true, icon: 'el-icon-circle-close', allowState: 1, api: machineManagerUpkeepMachineJobClearTaking },
                    // { code: 'CANCEL_FINISH', name: '撤销完成', permissionName: 'update', isShow: true, icon: 'el-icon-close', allowState: 2, api: machineManagerUpkeepMachineJobClearComplete }
                ],
                jumpDialogState: false,
                upkeepStateList: upkeepStateList,
                importDialogState: false,
                detailDialogState: false,
                dataId: null,
                saveDialogState: false,
                tableLoading: true,
                exportLoading: false,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: 1,
                    pageSize: pageConfig.pageSize,
                    code: null,
                    belongDate: [],
                    upkeepItemName: null,
                    workshopId: null,
                    machineNameOrCode: '',
                    upkeepState: 0
                }
            };
        },
        methods: {
            onDropdownChange(type, row) {
                const optionItem = this.dropdownOption.find(x => x.code === type);
                if (this.getPermission([`${this.prefix}:${optionItem.permissionName}`])) {
                    if (optionItem) {
                        if (optionItem.code === 'DETAIL') {
                            this.onDetailBtn({ id: row.id });
                        } else if (optionItem.code === 'JUMP') {
                            this.dataId = row.id;
                            this.jumpDialogState = true;
                        } else {
                            this.$confirm(`确定"${optionItem.name}"吗？`, '提示', { type: 'warning' }).then(() => {
                                optionItem.api({ id: row.id }).then(res => {
                                    if (res.data.status === 200) {
                                        this.getListRequest();
                                    }
                                });
                            }).catch(() => {});
                        }
                    }
                } else {
                    tipsBar(this, 'custom', 'warning', '无操作权限!');
                }
            },
            onFinishOfJumpDialog() {
                this.getListRequest();
            },
            onVisibleChangeOfJumpDialog(e) {
                this.jumpDialogState = e;
            },
            onTakeBtn(row) {
                this.$msgbox({
                    title: '消息',
                    message: `确定"保养签到"吗？`,
                    iconClass: 'el-icon-question',
                    showCancelButton: true,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    beforeClose: (action, instance, done) => {
                        if (action === 'confirm') {
                            instance.confirmButtonLoading = true;
                            instance.confirmButtonText = '执行中...';
                            machineManagerUpkeepMachineJobTaking({
                                id: row.id
                            }).then(res => {
                                instance.confirmButtonLoading = false;
                                instance.confirmButtonText = '确定';
                                if (res.data.status === 200) {
                                    done();
                                    this.getListRequest();
                                }
                            });
                        } else {
                            done();
                        }
                    }
                });
            },
            onVisibleChangeOfImportDialog(e) {
                this.importDialogState = e;
            },
            onFinishBtnOfImportDialog() {
                this.importDialogState = false;
                this.getListRequest();
            },
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            onDetailBtn(e) {
                this.dataId = e.id;
                this.detailDialogState = true;
            },
            onVisibleChangeOfDetailDialog(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.detailDialogState = e;
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.saveDialogState = e;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                this.apiItem.page(params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res || [];
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.dataId = row.id;
                this.saveDialogState = true;
            },

            /** 删除按钮操作 */
            onDeleteBtn(row) {
                this.apiItem.delete({ id: row.id }).then((res) => {
                    if (res.data.status === 200) {
                        this.getListRequest();
                        this.$tipsBar(this, 'delete');
                    }
                }).catch(() => {});
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            getMenuColumnListRequest() {
                return systemUserColumnConfigGet({ path: firstCharacterToLowerCase(this.$route.name), filterShow: true }).then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res.columnConfigList || [];
                        const arr = resData.find(x => x.code === 'task-view') ? resData.find(x => x.code === 'task-view').itemList : [];
                        this.tableColumnList = arr;
                    }
                });
            },
            async getDependentData() {
                await this.getMenuColumnListRequest();
                await this.getListRequest();
            }
        },
        mounted() {
            this.getDependentData();
        },
        watch: {
            queryParams: {
                handler(newVal) {
                    this.searchParams = newVal ? JSON.parse(JSON.stringify(newVal)) : {};
                },
                deep: true
            }
        }
    };
</script>
